2817. 限制条件下元素之间的最小绝对差
为保证权益,题目请参考 2817. 限制条件下元素之间的最小绝对差(From LeetCode).
解决方案1
CPP
C++
#include <algorithm>
#include <functional>
#include <iostream>
#include <limits>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
class Solution
{
public:
int minAbsoluteDifference(vector<int> &nums, int x)
{
set<int> s{numeric_limits<int>::max() / 2, numeric_limits<int>::min() / 2};
int ans = numeric_limits<int>::max();
for (int i = x; i < nums.size(); i++)
{
s.insert(nums[i - x]);
int y = nums[i];
set<int>::iterator it = s.lower_bound(y);
ans = min(ans, min(*it - y, y - *(--it)));
}
return ans;
}
};
int main()
{
Solution so;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34